﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Sharp.DataTransferObject.CMS;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.Platform.Mvc.Models;
using Sharp.ServiceContracts.CMS;
using Sharp.WebBooter.Areas.CMS.Models;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    public class TagController : Controller
    {
        // GET: Tag
        public ActionResult Index()
        {
            IEnumerable<VIUCategoryDTO> list;
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                list = proxy.Channel.GetCategoryList(treeData: false).Data;
            }
            var newList = new List<CategoryIndexViewModel>();
            Build(list, newList, null);

            ViewBag.Categories = newList;
            return View();
        }

        private void Build(IEnumerable<VIUCategoryDTO> table, List<CategoryIndexViewModel> node, Guid? parentValue)
        {
            var rows = table.Where(x => x.ParentId == parentValue).OrderBy(x => x.OrderIndex);
            foreach (var row in rows)
            {
                var _node = new CategoryIndexViewModel();
                _node.Name = row.Name;
                _node.Id = row.Id;
                _node.ParentId = row.ParentId;
                _node.Remark = row.Remark;
                if (row.ParentId == null)
                {
                    _node.Itemclass = string.Format("treegrid-{0}", row.Id);
                }
                else
                {
                    _node.Itemclass = string.Format("treegrid-{0} treegrid-parent-{1}", row.Id, row.ParentId);
                }


                node.Add(_node);

                Build(table, node, row.Id);
            }
        }
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="param"></param>
        /// <param name="propertyId">属性Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPaged(DataTableParameter param, Guid? propertyId)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var page = proxy.Channel.GetTagPaged(param.ToPagingFeature());

                return this.Store(page);
            }
        }


        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var model = new TagViewModel();
                model.TreeNode = new zTreeNode();
                model.Category = new VIUCategoryDTO();
                Build(proxy.Channel.GetCategoryList(treeData: false).Data, model.TreeNode, null);

                return View("Edit", model);
            }
        }


        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var model = proxy.Channel.GetTagInfo(id).Data.To<TagViewModel>();
                model.TreeNode = new zTreeNode();

                Build(proxy.Channel.GetCategoryList(treeData: false).Data, model.TreeNode, null);

                return View("Edit", model);
            }
        }
        private void Build(IEnumerable<VIUCategoryDTO> table, zTreeNode node, Guid? parentValue)
        {
            var rows = table.Where(x => x.ParentId == parentValue).OrderBy(x => x.OrderIndex);
            foreach (var row in rows)
            {
                var _node = new zTreeNode
                {
                    Name = row.Name,
                    Id = row.Id.ToString(),
                    ParentId = row.ParentId == null ? row.ParentId.ToString() : null,
                    Raw = row
                };

                node.Children.Add(_node);
                node.IsParent = node.Children.Any();

                Build(table, _node, row.Id);
            }
        }

        [HttpPost]
        public ActionResult Save(TagDTO dto)
        {
            if (dto == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }


            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.SaveTag(dto);

                return this.Json(res);
            }
        }

        [HttpPost]
        [Permission(Name = "删除", Order = 3)]
        public ActionResult Delete(Guid[] ids, FormCollection collection)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.DeleteTags(ids);
                return this.JsonNet(res);
            }
        }
    }
}